package com.google.code.UtopiaXml;

import java.util.ArrayList;
import java.util.List;

/**
 * Class representing an enumeration type. It consists of enum constants. It
 * should be generated only for enumerations with a base of string or similar,
 * because enum constants cannot begin with digits
 *
 * @author Michael Simacek
 */
public class EnumerationType implements SimpleType, ClassTemplate {

    private List<String> constants;
    private String name;

    /**
     * Creates empty enumeration type
     *
     * @param name name of the type
     */
    public EnumerationType(String name) {
        this.name = CodeBuilder.toUpperCamelCase(name);
        constants = new ArrayList<>();
    }

    /**
     * Adds single enum constant
     *
     * @param constant original (present in schema) name of the enumeration
     * constant
     */
    public void addEnumConstant(String constant) {
        constants.add(constant);
    }

    @Override
    public void appendLoaderFromString(CodeBuilder cb, String string, String resName) {
        cb.appendLine("//begin EnumerationType");
        cb.appendLine(string + " = " + string + ".toUpperCase().replace(\"-\", \"_\").replace(\" \", \"_\");");
        cb.appendLine(name + " " + resName + " = " + name + ".valueOf("
                      + string + ");");
        cb.appendLine("//end EnumerationType");
    }

    @Override
    public String getTypeName() {
        return name;
    }

    @Override
    public String getImport() {
        return "";
    }

    @Override
    public String getDefaultValue() {
        return "null";
    }

    @Override
    public void appendLoaderFromElement(CodeBuilder cb, String nodeName, String resName) {
        cb.appendLine("//begin EnumerationType");
        cb.appendLine(name + " " + resName + " = null;");
        cb.appendLine("if (" + nodeName + ".getFirstChild() != null) {");
        cb.appendLine(resName + " = " + name + ".valueOf("
                      + nodeName + ".getFirstChild().getNodeValue()."
                      + "toUpperCase().replace(\"-\", \"_\").replace(\" \", \"_\"));");
        cb.appendLine("}");
        cb.appendLine("//end EnumerationType");
    }

    @Override
    public ClassBuilder generateClass(String pkgName) {
        ClassBuilder clb = new ClassBuilder(name, pkgName);
        clb.setEnum();
        CodeBuilder cb = clb.getClassContentBuilder();

        StringBuilder constBuilder = new StringBuilder();
        for (String c : constants) {
            constBuilder.append(c.toUpperCase().replace("-", "_").replace(" ", "_"));
            constBuilder.append(", ");
        }
        if (constBuilder.length() > 2) {
            constBuilder.setLength(constBuilder.length() - 2);
        }
        constBuilder.append(";");
        cb.appendLine(constBuilder.toString());

        return clb;
    }

    @Override
    public void generateParseMethod(CodeBuilder cb) {
    }

    @Override
    public SimpleType copy() {
        EnumerationType ret = new EnumerationType(name);
        ret.constants = new ArrayList<>(constants);
        return ret;
    }
}
